package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 购物车前端控制器
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping(value = "/add",name = "添加购物车")
    public ResultInfo add(@RequestBody Cart cartParam) {
        Cart dbcart = cartService.add(cartParam);
        return ResultInfo.success(dbcart);
    }

    @GetMapping(value = "/list",name = "查询购物车")
    public ResultInfo list(){
        List<Cart> cartList = cartService.findAll();
        return ResultInfo.success(cartList);
    }

    @PostMapping(value = "/sub",name = "修改购物车")
    public ResultInfo subUpdate(@RequestBody Cart cartParam){
        Cart cart =cartService.subUpdate(cartParam);
        //判断购物车是否为空
        if (cart==null){
            cart = new Cart();
            cart.setNumber(0);
        }
        //购物车不为空
        return ResultInfo.success(cart);
    }

    @DeleteMapping(value = "/clean")
    public ResultInfo clean(){
        cartService.clean();
        return ResultInfo.success(null);
    }
}
